# coding:utf-8
from typing import Union, Callable

from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QWidget, QLabel, QVBoxLayout, QHBoxLayout
from qfluentwidgets import IconWidget, FlowLayout, CardWidget, FluentIconBase, ImageLabel

from common import signalBus, StyleSheet


class SampleCard(CardWidget):
    """ Sample card """
    clicked = pyqtSignal()

    def __init__(self, icon: Union[str, QIcon, FluentIconBase], title: str, content: str, parent=None):
        super().__init__(parent=parent)
        if isinstance(icon, FluentIconBase):
            icon = icon.path()
        elif isinstance(icon, QIcon):
            icon = icon.pixmap(48, 48).toImage()
        self.iconWidget = ImageLabel(icon, self)
        self.titleLabel = QLabel(title, self)
        self.contentLabel = QLabel(content, self)

        self.hBoxLayout = QHBoxLayout(self)
        self.vBoxLayout = QVBoxLayout()

        self.setClickEnabled(True)
        self.setFixedSize(360, 90)
        self.iconWidget.setFixedSize(48, 48)
        self.titleLabel.setWordWrap(True)
        self.contentLabel.setWordWrap(True)

        self.hBoxLayout.setSpacing(20)
        self.hBoxLayout.setContentsMargins(20, 0, 20, 0)
        self.vBoxLayout.setSpacing(2)
        self.vBoxLayout.setContentsMargins(0, 0, 0, 0)
        self.vBoxLayout.setAlignment(Qt.AlignmentFlag.AlignVCenter)

        self.hBoxLayout.setAlignment(Qt.AlignmentFlag.AlignVCenter)
        self.hBoxLayout.addWidget(self.iconWidget)
        self.hBoxLayout.addLayout(self.vBoxLayout)
        self.vBoxLayout.addStretch(1)
        self.vBoxLayout.addWidget(self.titleLabel)
        self.vBoxLayout.addWidget(self.contentLabel)
        self.vBoxLayout.addStretch(1)

        self.titleLabel.setObjectName('titleLabel')
        self.contentLabel.setObjectName('contentLabel')
        self.iconWidget.scaledToHeight(48)
        StyleSheet.SAMPLE_CARD.apply(self)

    def mouseReleaseEvent(self, e):
        if e.button() == Qt.MouseButton.LeftButton and self.isClickEnabled():
            self.clicked.emit()

